package com.hxzy.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.yygh.cmn.client.DictFeignClient;
import com.hxzy.yygh.enums.DictEnum;
import com.hxzy.yygh.model.user.Patient;
import com.hxzy.yygh.user.mapper.PatientMapper;
import com.hxzy.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    private PatientMapper patientMapper;

    @Autowired
    private DictFeignClient dictFeignClient;

    //获得就诊人列表方法
    @Override
    public List<Patient> getPatientList(Long userId) {
        //传入user，构造条件查询出就诊人集合
        LambdaQueryWrapper<Patient> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Patient::getUserId, userId);
        List<Patient> patientList = patientMapper.selectList(lqw);

        //修改就诊人信息(编码  =>>  名字)
        patientList.stream().forEach(item ->{
            //调用封装参数的方法
            this.packPatientParams(item);
        });

        //返回处理好的就诊人集合
        return patientList;
    }

    //根据id查询就诊人信息方法
    @Override
    public Patient getPatientById(Long id) {
        //根据id查询出就诊人信息
        Patient patient = patientMapper.selectById(id);
        //返回处理好的就诊人信息
        return this.packPatientParams(patient);
    }

    //根据数据字典，将编号 => 名称
    private Patient packPatientParams(Patient patient) {

        //根据就诊人证件类型编码 查询对应的名称
        String certificatesTypeName = dictFeignClient.getDictName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());

        //根据联系人证件类型编码 查询对应的名称
        String contactsCertificatesType = patient.getContactsCertificatesType();
        String contactsCertificatesTypeName;
        if(!StringUtils.isEmpty(contactsCertificatesType)){
            contactsCertificatesTypeName = dictFeignClient.getDictName(DictEnum.CERTIFICATES_TYPE.getDictCode(), contactsCertificatesType);
        }else {
            contactsCertificatesTypeName = " ";
        }


        //根据省级编码 查询对应的省级名称
        String provinceName = dictFeignClient.getDictName(patient.getProvinceCode());

        //根据市级编码 查询对应的省级名称
        String cityName = dictFeignClient.getDictName(patient.getCityCode());

        //根据区级编码 查询对应的省级名称
        String districtCodeName = dictFeignClient.getDictName(patient.getDistrictCode());

        //封装参数到patient的param属性中
        patient.getParam().put("certificatesTypeName", certificatesTypeName);
        patient.getParam().put("contactsCertificatesTypeName", contactsCertificatesTypeName);
        patient.getParam().put("provinceName", provinceName);
        patient.getParam().put("cityName", cityName);
        patient.getParam().put("districtCodeName", districtCodeName);
        patient.getParam().put("fullAddress", provinceName + cityName + districtCodeName + patient.getAddress());

        //返回处理好的就诊人数据
        return patient;
    }
}
